<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>信息管理</el-breadcrumb-item>
      <el-breadcrumb-item>房屋信息</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row>
        <el-col :span="5">
          <el-input placeholder="请输入建筑名称" v-model="queryInfo.houseName" clearable @clear="getHouse">
            <el-button slot="append" icon="el-icon-search" @click="getHouse"></el-button>
          </el-input>
        </el-col>
        <el-col :span="3" style="padding-left:20px">
          <el-select v-model="queryInfo.community" placeholder="社区" @change="getHouse">
            <el-option v-for="item in cityData" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-col>
        <el-col :span="4" style="padding-left:20px">
          <el-button type="primary" @click="addDialogVisible=true" v-if="this.myidentity==='1'">添加房屋信息</el-button>
        </el-col>
      </el-row>
      <el-table :data="houseList" border>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="房屋名称" prop="houseName"></el-table-column>
        <el-table-column label="所在社区" prop="community"></el-table-column>
        <el-table-column label="详细地址" prop="address"></el-table-column>
        <el-table-column label="居住人数" prop="num"></el-table-column>
        <el-table-column label="操作" v-if="this.myidentity==='1'">
          <template slot-scope="{row}" >
            <el-button type="primary" size="mini" @click="ShowEdit(row.houseId)">修改</el-button>
            <el-button type="warning" size="mini" @click="removeById(row.houseId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pageNum" :page-sizes="[5, 10, 30, 50]" :page-size="queryInfo.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" style="text-align:center" background>
      </el-pagination>
    </el-card>

    <el-dialog title="添加建筑信息" :visible.sync="addDialogVisible" width="50%" @close="resetAddForm">
      <el-form :model="addForm" ref="addFormRef" label-width="100px" :rules="rules">
        <el-form-item label="建筑名称" prop="houseName">
          <el-input v-model="addForm.houseName"></el-input>
        </el-form-item>
        <el-form-item label="所在社区" prop="community">
          <el-select v-model="addForm.community" placeholder="请选择所在社区">
            <el-option v-for="item in cityData" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="addForm.address"></el-input>
        </el-form-item>
        <el-form-item label="居住人数">
          <el-input v-model="addForm.num"></el-input>
        </el-form-item>
      </el-form>
      <span>
        <el-button @click="resetAddForm">取 消</el-button>
        <el-button type="primary" @click="addHouse">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="修改建筑信息" :visible.sync="editDialogVisible" width="50%" @close="resetAddForm">
      <el-form :model="editForm" ref="addFormRef" label-width="100px" :rules="rules">
        <el-form-item label="建筑名称" prop="houseName">
          <el-input v-model="editForm.houseName"></el-input>
        </el-form-item>
        <el-form-item label="所在社区" prop="community">
          <el-select v-model="editForm.community" placeholder="请选择所在社区">
            <el-option v-for="item in cityData" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="editForm.address"></el-input>
        </el-form-item>
        <el-form-item label="居住人数">
          <el-input v-model="editForm.num"></el-input>
        </el-form-item>
      </el-form>
      <span>
        <el-button @click="editDialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="editHouse">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import cityData from '../../assets/js/cityData.js'
export default {
  data () {
    return {
      myidentity: window.sessionStorage.getItem('identity'),
      houseList: [],
      queryInfo: {
        houseName: '',
        pageNum: 1,
        pageSize: 5,
        community: ''
      },
      total: 0,
      cityData,
      addDialogVisible: false,
      addForm: {
        houseName: '',
        address: '',
        community: '',
        num: ''
      },
      editDialogVisible: false,
      editForm: {
        houseName: '',
        community: '',
        num: '',
        address: ''
      },
      rules: {
        houseName: [{ required: true, message: '请输入建筑名称', trigger: 'blur' }],
        address: [{ required: true, message: '请输入详细地址', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.getHouse()
  },
  methods: {
    async getHouse () {
      const { data: res } = await this.$http.get('house/getHouse', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error('请求房屋列表数据失败')
      }
      this.$message.success('请求房屋列表数据成功')
      this.houseList = res.data.records
      this.total = res.data.total
    },
    // 分页器
    handleSizeChange (newSize) {
      this.queryInfo.pageSize = newSize
      this.getHouse()
    },
    handleCurrentChange (newSize) {
      this.queryInfo.pageNum = newSize
      this.getHouse()
    },
    async ShowEdit (id) {
      const { data: res } = await this.$http.get(`house/getHouseById/${id}`)
      this.editForm = res.data
      this.editDialogVisible = true
    },
    async editHouse () {
      const { data: res } = await this.$http.put(`house/editHouseById/${this.editForm.houseId}`, this.editForm)
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('修改失败')
      }
      // console.log(res)
      this.$message.success('修改成功')
      this.editDialogVisible = false
      this.getHouse()
    },
    async removeById (id) {
      const confirm = await this.$confirm('此操作将永久删除该建筑信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => {
        console.log(err)
      })
      if (confirm !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await this.$http.delete(`house/deleteHouseById/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('删除失败')
      }
      this.$message.success('删除成功')
      this.getHouse()
    },
    // 清空添加对话框
    resetAddForm () {
      this.$refs.addFormRef.resetFields()
      this.addDialogVisible = false
    },
    async addHouse () {
      const { data: res } = await this.$http.post('house/addHouse', this.addForm)
      // console.log(res)
      if (res.meta.status !== 200) {
        this.addDialogVisible = false
        return this.$message.error('添加失败')
      }
      // console.log(res)
      this.$message.success('成功')
      this.addDialogVisible = false
      this.getHouse()
    }
  }
}
</script>

<style lnag='less' scoped>
</style>
